package com.atguigu.service.impl;

import com.atguigu.base.BaseMapper;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.entity.AclAdmin;
import com.atguigu.entity.AdminRole;
import com.atguigu.mapper.AdminMapper;
import com.atguigu.mapper.AdminRoleMapper;
import com.atguigu.service.AdminService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;


@Service
public class AdminServiceImpl extends BaseServiceImpl<AclAdmin> implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private AdminRoleMapper adminRoleMapper;


    @Override
    public BaseMapper<AclAdmin> getBaseMapper() {
        return adminMapper;
    }

    @Override
    public void assignRole(Long adminId, Long[] roleIds) {
        //先将原来的删除在添加,如果改状态会很麻烦
        adminRoleMapper.deleteByAdminId(adminId);
        //添加
        for (Long roleId : roleIds) {
            AdminRole adminRole = new AdminRole();
            adminRole.setAdminId(adminId);
            adminRole.setRoleId(roleId);

            adminRoleMapper.insert(adminRole);
        }
    }

    @Override
    public AclAdmin getByUsername(String username) {
        return adminMapper.getByUsername(username);
    }

    @Override
    public String getRoleNameByAdminId(Long adminId) {
        return adminMapper.getRoleNameByAdminId(adminId);
    }
}
